package com.runda.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import tk.mybatis.mapper.entity.Example;

import com.bbjob.dto.Result;
import com.bbjob.mapper.ActivateCodeMapper;
import com.bbjob.mapper.CenterMacSigninMapper;
import com.bbjob.model.ActivateCode;
import com.bbjob.model.CenterMacSignin;
import com.github.pagehelper.PageInfo;
import com.runda.query.QueryActivateCode;
import com.runda.service.IActivateCodeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags="activateCodeApi", description="授权码管理")
@RestController
@RequestMapping(path="activatecode", produces="application/json;charset=utf-8")
public class ActivateCodeController {

	@Resource
	private IActivateCodeService activateCodeService;
	@Resource
	private CenterMacSigninMapper centerMacSigninMapper;
	@Resource
	private ActivateCodeMapper activateCodeMapper;
	
	@ApiOperation(value="授权码列表", notes="授权码列表")
	@ApiImplicitParam(name="activateCode", value="授权码信息", required=true, dataType="QueryActivateCode")
	@RequestMapping(value="list", method=RequestMethod.POST)
	public Result list(@RequestBody QueryActivateCode activateCode) {
		
		List<ActivateCode> data = activateCodeService.selectByParams(activateCode);
		PageInfo<ActivateCode> pageInfo = new PageInfo<ActivateCode>(data);
		
		return new Result(pageInfo);
	}
	
	
	@ApiOperation(value="新增生成授权码", notes="新增生成授权码")
	@ApiImplicitParam(name="activateCode", value="授权码信息", required=true, dataType="ActivateCode")
	@RequestMapping(value="add", method=RequestMethod.POST)
	public Result add(@RequestBody ActivateCode activateCode) {
		
		activateCode.setShadowCode(UUID.randomUUID().toString().replace("-", ""));
		activateCode.setCodeType(1);
		activateCode.setCreateTime(new Date());
		activateCode.setUpdateTime(new Date());
		activateCode.setNowTime(0);
		
		this.activateCodeService.save(activateCode);
		
		return new Result(activateCode);
	}
	
	
	@ApiOperation(value="修改授权码信息", notes="修改授权码信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name="id", value="授权码明码id", required=true, dataType="Integer", paramType="path"),
		@ApiImplicitParam(name="activateCode", value="授权码信息", required=true, dataType="ActivateCode")
	})
	@RequestMapping(value="edit/{id}", method=RequestMethod.POST)
	public Result edit(@PathVariable("id")Integer id, @RequestBody ActivateCode activateCode) {
		
		activateCode.setId(id);
		activateCode.setUpdateTime(new Date());

		ActivateCode activateCodeOri = activateCodeMapper.selectByPrimaryKey(id);
		
		Example example = new Example(CenterMacSignin.class);
		example.createCriteria().andEqualTo("accodeId", id);
		List<CenterMacSignin> signs = centerMacSigninMapper.selectByExample(example);
		if (signs.size() > 0) {
			if (activateCodeOri.getOrganId() != activateCode.getOrganId()){
				
				return new Result(8106, "该激活码已被学校激活，不允许更换学校");
			} 
		}
		
		if (activateCode.getUseTime() != null && activateCode.getUseTime() != 0) {
			if (activateCode.getUseTime() < activateCodeOri.getNowTime()) {
				return new Result(8106, "修改失败，已激活次数大于可用次数");
			}
		}
		
		this.activateCodeService.updateNotNull(activateCode);
		
		return new Result();
	}
	
	
	@ApiOperation(value="获取单条授权码信息", notes="获取单条授权码信息")
	@ApiImplicitParam(name="id", value="授权码信息id", required=true, dataType="Integer", paramType="path")
	@RequestMapping(value="get/{id}", method=RequestMethod.GET)
	public Result get(@PathVariable("id")Integer id) {
		
		ActivateCode activateCode = this.activateCodeService.selectByKey(id);
		
		return new Result(activateCode);
	}
	
	
	@ApiOperation(value="删除单条授权码信息", notes="删除单条授权码信息")
	@ApiImplicitParam(name="id", value="授权码信息id", required=true, dataType="Integer", paramType="path")
	@RequestMapping(value="del/{id}", method=RequestMethod.GET)
	public Result del(@PathVariable("id")Integer id) {
		
		Example example = new Example(CenterMacSignin.class);
		example.createCriteria().andEqualTo("accodeId", id);
		List<CenterMacSignin> signs = centerMacSigninMapper.selectByExample(example);
		if (signs.size() > 0) {
			return new Result(8106, "该激活码已被学校激活，不允许删除");
		}
		this.activateCodeService.delete(id);
		
		return new Result();
	}
}
